package com.collectplus.express.view;

import java.util.ArrayList;

import android.view.View;
import android.view.ViewGroup;

/**
 * 自定义 adapter 不带缓存<br>
 * 
 * @author coffee<br>
 *         2016-12-27下午5:18:56
 */
public class ViewGroupAdapter<T> {

	private ViewGroup viewGroup;

	private ArrayList<T> items;

	public ViewGroupAdapter(ArrayList<T> items, ViewGroup viewGroup) {
		this.items = items;
		this.viewGroup = viewGroup;
	}

	public void setItems(ArrayList<T> items, boolean notifyDataSetChanged) {
		this.items = items;
		if (notifyDataSetChanged) {
			notifyDataSetChanged();
		}
	}

	public void notifyDataSetChanged() {
		if (items == null || items.isEmpty()) {
			viewGroup.removeAllViews();
			return;
		}
		for (int i = 0; i < items.size(); i++) {
			// 刷新
			if (i <= viewGroup.getChildCount() - 1) {
				View child = getView(i, null, viewGroup);
				viewGroup.removeViewAt(i);
				viewGroup.addView(child, i);
			} else {// items个数 > viewGroup的child数
				View child = getView(i, null, viewGroup);
				viewGroup.addView(child, i);
			}
		}
		// 删除
		if (viewGroup.getChildCount() > items.size()) {
			viewGroup.removeViews(items.size(), viewGroup.getChildCount() - items.size());
		}
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		return null;
	}

	public int getCount() {
		if (items == null) {
			return 0;
		}
		return items.size();
	}

	public T getItem(int position) {
		return items.get(position);
	}
}
